package com.gen.utils;

import okhttp3.*;
import org.apache.http.conn.ssl.AllowAllHostnameVerifier;

import java.io.IOException;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;

public class HttpUtils {

	// 实例化Http请求
	static OkHttpClient client = new OkHttpClient();

    // 请求头
    public static final MediaType JSON = MediaType.parse("application/json; charset=utf-8");

    // GET方式请求
    public static String GET(String url) throws IOException {
        Request request = new Request.Builder().url(url).build();
        Response response = client.newCall(request).execute();
        return response.body().string();
    }

    static OkHttpClient clientSSL = new OkHttpClient().newBuilder()
            .readTimeout(3000, TimeUnit.MILLISECONDS)
            .writeTimeout(3000, TimeUnit.MILLISECONDS)
            .connectTimeout(3000, TimeUnit.MILLISECONDS)
            .sslSocketFactory(SSLSocketClient.getSSLSocketFactory())
            .hostnameVerifier(SSLSocketClient.getHostnameVerifier())
            .hostnameVerifier(new AllowAllHostnameVerifier())
            .build();

    /**
     *
     * @param url
     * @param json
     * @return
     * @throws IOException
     */
    public static String POSTJSON(String url, String json) throws IOException {
        RequestBody body = RequestBody.create(JSON, json);
        Request request = new Request.Builder().url(url).post(body).build();
        Response response = client.newCall(request).execute();
        return response.body().string();
    }

    /**
     * 参数提交
     * @param url
     * @param params
     * @return
     * @throws IOException
     */
    public static String post(String url, Map<String, String> params) throws IOException {
        FormBody.Builder builder = new FormBody.Builder();
        Set<Map.Entry<String, String>> set = params.entrySet();
        Iterator<Map.Entry<String, String>> it = set.iterator();
        while (it.hasNext()) {
            Map.Entry entry = it.next();
            builder.add(entry.getKey() + "", entry.getValue() + "");
        }
        RequestBody formBody = builder.build();

        Request request = new Request.Builder()
                .url(url)
                .post(formBody)
                .build();

        Response response = client.newCall(request).execute();
        if (response.isSuccessful()) {
            return response.body().string();
        } else {
            throw new IOException("Unexpected code " + response);
        }
    }

    /**
     * 参数提交
     * @param url
     * @param params
     * @return
     * @throws IOException
     */
    public static String postMap(String url, Map<String, String> params) throws IOException {
        FormBody.Builder builder = new FormBody.Builder();
        Set<Map.Entry<String, String>> set = params.entrySet();
        Iterator<Map.Entry<String, String>> it = set.iterator();
        while (it.hasNext()) {
            Map.Entry entry = it.next();
            builder.add(entry.getKey() + "", entry.getValue() + "");
        }
        RequestBody formBody = builder.build();

        Request request = new Request.Builder()
                .url(url)
                .post(formBody)
                .build();

        Response response = clientSSL.newCall(request).execute();
        if (response.isSuccessful()) {
            return response.body().string();
        } else {
            throw new IOException("Unexpected code " + response);
        }
    }

    /**
     * 参数提交
     * @param url
     * @param params
     * @return
     * @throws IOException
     */
    public static String postMap(String url,String MerchantId,String publickey, Map<String, String> params) throws IOException {
        FormBody.Builder builder = new FormBody.Builder();
        Set<Map.Entry<String, String>> set = params.entrySet();
        Iterator<Map.Entry<String, String>> it = set.iterator();
        while (it.hasNext()) {
            Map.Entry entry = it.next();
            builder.add(entry.getKey() + "", entry.getValue() + "");
        }
        RequestBody formBody = builder.build();

        Request request = new Request.Builder()
                .url(url)
                .post(formBody)
                .addHeader("MerchantId",MerchantId)
                .addHeader("Content-Type","application/json")
                .addHeader("Authorization","Bearer "+ publickey)
                .build();

        Response response = client.newCall(request).execute();
        if (response.isSuccessful()) {
            return response.body().string();
        } else {
            throw new IOException("Unexpected code " + response);
        }
    }

}
